class SecurityGroupsController < ApplicationController
  layout nil
  
  def index
    respond_to do |format| 
      format.html {
        render
      }
      
      format.json {
        groups = []
        user = User.find(session[:current_user_id])
        Ec2Factory.create(user, session[:url]).describe_security_groups.each_with_index{|rawGroup, i|
          if rawGroup[:aws_owner] == user.name then
            groups << {:id => i + 1, :cell => [
              rawGroup[:aws_group_name], 
              rawGroup[:aws_owner],
            ]}
          end
          
          
        }

        render :json => {
          :page => params[:page],
          :total => groups.length / params[:rows].to_i + 1,
          :records => groups.length,
          :rows => groups 
        }.to_json
      }
    end
  end
  
  def name
    respond_to do |format| 
      format.json {
        names = []
        user = User.find(session[:current_user_id])
        Ec2Factory.create(user, session[:url]).describe_security_groups.each{|rawGroup|
          if rawGroup[:aws_owner] == user.name then
              names << rawGroup[:aws_group_name]
          end
        }

        render :json => names.to_json
      }
    end
  end
end
